#!/bash/sh

# ------ 变量 ------
DEPLOY_DIR=/xdfapp/study-java-jvm
JAR_FILE=$DEPLOY_DIR/study-java-jvm-1.0.jar

# ------ 停止服务 ------
pid=$(ps -ef | grep -v grep | grep $JAR_FILE | awk '{print $2}')
if [ ! -z "$pid" ]; then
  kill -9 $pid >/dev/null
  sleep 2
  echo '[INFO] Service Stopped'
fi

# ------ 参数 ------
# 内存
#JAVA_MEM_OPTS=" -server -Xms100M -Xmx100M"
#JAVA_MEM_OPTS=" -server -Xms100M -Xmx100M -Xmn50M"
#JAVA_MEM_OPTS=" -server -Xms100M -Xmx100M -XX:NewRatio=2 -XX:SurvivorRatio=8"
JAVA_MEM_OPTS=" -server -Xms100M -Xmx100M -XX:MetaspaceSize=50M -XX:MaxMetaspaceSize=50M"
# GC
JAVA_GC_OPTS=" -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintHeapAtGC -Xloggc:/xdfapp/logs/study-java-jvm/gc.log"
# JMX
JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=172.24.33.48 -Dcom.sun.management.jmxremote.rmi.port=9999"

JAVA_OPTS=$JAVA_MEM_OPTS$JAVA_GC_OPTS

# ------ 启动服务 ------
source /etc/profile
#java $JAVA_OPTS -jar $JAR_FILE
nohup java $JAVA_OPTS -jar $JAR_FILE >/dev/null 2>&1 &
if [ $? -eq 0 ];then
  echo '[INFO] service started'
else
  echo '[ERROR] service startup error'
fi